<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/7/15
 * Time: 18:02
 */

namespace app\admin\logic;


use app\admin\logic\code\ErrorCode;
use app\common\model\Admin;
use app\common\model\Driver;

class DriverLogic extends BaseLogic
{

    private $driverModel;

    public function __construct()
    {
        parent::__construct();
    }

    public function busList(){
        $role = session('admin_info');
        if ($role['role_id']==2){
            $list = Admin::where('id','eq',$role['id'])->select();
        }else{
            $list = Admin::where('role_id','eq',2)->select();
        }
        return $list;
    }

    public function driverList($tempWhere, $nowPage){
        $role = session('admin_info');
        if ($tempWhere['shop_id']){
            $where[] = ['shop_id', '=', $tempWhere['shop_id']];
        }
        if ($role['role_id']==2){
            $where[] = ['shop_id', 'eq', $role['id']];
        }
        if ($tempWhere['name']){
            $where[] = ['name', 'like', "%".$tempWhere['name']."%"];
        }

        $count = Driver::where($where)->count();
        if ($count <= 0){
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }

        $list = Driver::with('business')->where($where)->page($nowPage, self::$pageNum)->order('id desc')->select();
        return ajaxArr(ErrorCode::$SUCCESS, 'success', $list);
    }

    /**
     * 司机信息
     * @param $driverId
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function driverInfo($driverId){
        if ($driverId){
            $where[] = ['id', '=', $driverId];
            $driverInfo = Driver::where($where)->find();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $driverInfo);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '该司机信息不存在');
        }
    }

    public function handle($data){
        $act = $data['act'];
        $res = false;
        switch ($act){
            case 'add':
                $insert['shop_id'] = $data['shop_id'];
                $insert['name'] = $data['name'];
                $insert['mobile'] = $data['mobile'];
                $insert['create_time'] = time();
                $res = Driver::insert($insert);
                break;
            case 'edit':
                $update['shop_id'] = $data['shop_id'];
                $update['name'] = $data['name'];
                $update['mobile'] = $data['mobile'];
                $update['update_time'] = time();
                $res = Driver::where('id','eq',$data['id'])->update($update);
                break;
            case 'del':
                $info = BusDriver::get($data['id']);
                $res = $info->delete();
                break;
            case 'state':
                if (!is_numeric($data['status'])){
                    return ajaxArr(ErrorCode::$OPERATIONERROR, '请先选择审核状态');
                }
                $info = BusDriver::get($data['id']);
                $info->is_state = 1;
                $info->status = $data['status'];
                $res = $info->save();
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

}